package site.lyweb.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import site.lyweb.domain.dto.Course;
import site.lyweb.domain.dto.CourseDTO;
import site.lyweb.domain.dto.PageDTO;
import site.lyweb.domain.query.CourseQuery;
import site.lyweb.result.RestResult;
import site.lyweb.service.CourseService;
import site.lyweb.utils.UUIDUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @Author 林瑶
 * @Description TODO
 * @Date 2024/6/20 0:01
 */
@CrossOrigin
@RestController
@Tag(name="课程管理模块")
@Slf4j
@RequestMapping("/course")
public class CourseController {
    @Resource
    private CourseService courseService;

    /**
     * 根据id查找课程信息表中信息
     */
    @Operation(summary = "根据id查找课程信息表中信息")
    @Parameters({
            @Parameter(name = "id",description = "id(主键)",in = ParameterIn.PATH)
    })
    @GetMapping("/course_info/getById/{id}")
    public RestResult queryCourseById(@PathVariable("id") String id){
        Course byId = courseService.getById(id);

        return RestResult.SUCCESS(byId);
    }
    /**
     * 查找全部课程信息表中信息
     */
    @Operation(summary = "查找全部课程信息表中信息")
    @GetMapping("/course_info/getAll")
    public RestResult queryCourseAll(){
        return RestResult.SUCCESS(courseService.list());
    }
    /**
     * 根据id删除课程信息表中信息
     */
    @Operation(summary = "根据id删除学生课程表中信息")
    @Parameters({
            @Parameter(name = "id",description = "id(主键)",in = ParameterIn.PATH)
    })
    @PostMapping("/course_info/del/{id}")
    public RestResult delCourseById(@PathVariable("id") String id){
        return courseService.removeById(id) ?RestResult.SUCCESS("删除学生信息表中信息成功"):RestResult.FAIL("删除学生信息表中信息失败");
    }

    /**
     * 保存课程信息表中信息
     */
    @Operation(summary = "保存课程信息表中信息")
    @PostMapping("/course_info/edit")
    @Parameters({
            @Parameter(name = "id",description = "id（主键）",in = ParameterIn.QUERY),
            @Parameter(name = "name",description = "课程名",in = ParameterIn.QUERY),
            @Parameter(name = "credits",description = "学分",in = ParameterIn.QUERY),
    })
    public RestResult editCourse(Course course){
        if(course!=null){
            //有id：修改，无id:添加
            if(course.getId()!=null && !course.getId().isEmpty()){
                return courseService.updateById(course)?RestResult.SUCCESS("修改成功："+course):RestResult.FAIL();
            }else{
                course.setId(UUIDUtils.getUUID());
                return courseService.save(course)?RestResult.SUCCESS("添加成功："+course):RestResult.FAIL();
            }
        }
        return RestResult.FAIL("传入学生对象为空，保存失败");
    }

    /**
     * 分页查询课程列表数据
     *
     * @return
     */

    @Operation(summary = "分页查询课程列表数据")
    @GetMapping(value = "/course_info/getCourseByPage")
    public RestResult getCourseByPage( CourseQuery courseQuery) {


        //分页查询的结果用PageDTO进行封装
        PageDTO<CourseDTO> pageDTO = courseService.getCourseByPage(0, courseQuery);

        //有的时候，查询可以不判断成功与失败，不管查没查到数据，都返回200，页面上去控制显示
        return RestResult.SUCCESS(pageDTO);
    }
    /**
     * 根据id批量课程信息表中信息
     */
    @Operation(summary = "根据ids删除课程信息表中信息")
    @Parameters({
            @Parameter(name = "ids",description = "id(主键)",in = ParameterIn.PATH)
    })
    @CrossOrigin
    @PostMapping("/course_info/batchDel/{ids}")
    public RestResult batchDelCourseById(@PathVariable("ids") String ids){
        //ids = "1,2,5,7,10,12" , ids.split(",")是把ids转成数组, Arrays.asList()是把数组转成List
        List<String> idList = Arrays.asList(ids.split(","));

        return  courseService.removeBatchByIds(idList) ? RestResult.SUCCESS() : RestResult.FAIL();
    }
}
